package com.zboot.zbootadmin.common.rest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice(annotations = RestController.class)
public class RestExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorEntity> resourceNotFoundException(HttpServletRequest request, Exception ex) {
        ErrorEntity entity = new ErrorEntity();
        entity.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        entity.setError(HttpStatus.INTERNAL_SERVER_ERROR.name());
        entity.setTimestamp(System.currentTimeMillis());
        entity.setExceptionClass(ex.getClass().getName());
        entity.setMessage(ex.getMessage());
        entity.setPath(request.getRequestURL().toString());
        return new ResponseEntity<>(entity, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
